package com.lyf.shop.web.controller;

import com.alibaba.fastjson.JSON;
import com.lyf.shop.common.Const;
import com.lyf.shop.entity.*;
import com.lyf.shop.services.AddressService;
import com.lyf.shop.services.CartService;
import com.lyf.shop.services.Impl.AddressServiceImpl;
import com.lyf.shop.services.Impl.CartServiceImpl;
import com.lyf.shop.services.Impl.OrderServiceImpl;
import com.lyf.shop.services.OrderService;
import com.lyf.shop.util.StrUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/order")
public class OrderController extends BaseController{
    private CartService cartService = new CartServiceImpl();
    private AddressService addressService = new AddressServiceImpl();
    private OrderService orderService = new OrderServiceImpl();

    public String afterPay(HttpServletRequest request,HttpServletResponse response){
        String oid = request.getParameter("oid");
        String result = request.getParameter("result");
        //result JSON字符串
        WeiXin weiXin = JSON.parseObject(result, WeiXin.class);

        String res = weiXin.getResult().getResult_code();
        if(StrUtils.empty(res)&&res.equals("SUCCESS")){
            //支付成功 修改订单状态已支付
            orderService.updateOrderStateToPaySuccess(oid);
            return  Const.REDIRECT+"/order?method=showOrderList";
        }else {
            request.setAttribute("msg","支付失败");
            return Const.FORWARD+"/message.jsp";
        }

    }

    /**
     * 获取订单详情
     * @param request
     * @param response
     * @return
     */
    public String getOrderDetail(HttpServletRequest request,HttpServletResponse response){
        //参数接受 oid订单编号
        String oid = request.getParameter("oid");//本身数据是字符串
        //使用oid来查询order的数据和List<Item>
        List<Item> itemByOid = orderService.getItemByOid(oid);
        Orders orderByOid = orderService.getOrderByOid(oid);
        //存值到request中
        request.setAttribute("order",orderByOid);
        request.setAttribute("itemList",itemByOid);
        return Const.FORWARD+"/orderDetail.jsp";
    }

    /**
     * 获取订单数据
     * @param request
     * @param response
     * @return
     */
    public String showOrderList(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute(Const.UserConst.LOGIN_USER);
        if(user==null){
            return Const.REDIRECT+"/login.jsp";
        }
        //访问业务层进行数据获取，使用参数uid
        List<Orders> ordersList = orderService.GetOrderList(user.getUid());
     //  for (Orders orders : ordersList) {
     //      System.out.println(orders);
     //  }
        request.setAttribute("orderList",ordersList);
        return Const.FORWARD+"/orderList.jsp";
    }

    /**
     * 创建订单
     * @param request
     * @param response
     * @return
     */
    public String createOrder(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute(Const.UserConst.LOGIN_USER);
        if(user==null){
            return Const.REDIRECT+"/login.jsp";
        }
        //前端值给了aid
        //只需要使用uid和aid去创建一个订单数据
        Integer aid = Integer.valueOf(request.getParameter("aid"));
        //把uid和aid交给业务层去创建订单
        orderService.createOrder(aid,user.getUid());
        //创建订单完成后去订单详情列表中展示
        return Const.REDIRECT+"/orderList.jsp";
    }


    /**
     * 获取购物车还有地址的数据，转发到order.jsp里面进行展示
     * @param request
     * @param response
     * @return
     */
    public String showView(HttpServletRequest request, HttpServletResponse response){
        User user = (User) request.getSession().getAttribute(Const.UserConst.LOGIN_USER);
        if(user==null){
            return Const.REDIRECT+"/login.jsp";
        }
        //使用uid来查询购物车的数据，收货地址的数据
        //c查询用户名下所有的购物车信息
        List<Cart> cartList = cartService.findCart(user.getUid());
        List<Address> addressList = addressService.getAddressByUid(user.getUid());
        request.setAttribute("cartList",cartList);
        request.setAttribute("addList",addressList);
        return Const.FORWARD+"/order.jsp";
    }
}
